package com.cyg;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * 代码自动生成器
 */
public class CodeGenerator {

    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mariadb://120.79.207.46:3306/mybatis_plus?useSSL=false&userUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "paradb", "supermas");

    public static void main(String[] args) throws SQLException {
        // 生成到项目目录
        String pkgPath = System.getProperty("user.dir") + "/src/main/java";

        // 指定xml文件位置
        String pkgXml = System.getProperty("user.dir") + "/src/main/resources/mapper";

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig(builder ->
                        builder.author("BecomeBamboo")
                                .outputDir(pkgPath)
                                .disableOpenDir()  // 生成后不打开窗口
                )

                // 包配置  com.cyg和com.baomidou两个package
                .packageConfig(builder ->
                        builder.parent("com.baomidou")
                                .entity("vo")  // 实体类包名
                                .pathInfo(new HashMap<OutputFile, String>() {{
                                    put(OutputFile.xml, pkgXml);
                                }})
                )

                // 策略配置
                .strategyConfig(builder ->
                        builder.addInclude("blog"))
                .execute();

    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
